<?php
function search_url($arr=[]){
    $url_path = '/'.request()->path();
    $get = input('get.');
    array_push($arr, "_pjax");
    if(!empty($get)){
        $paramStr = [];
        foreach ($get as $k=>$v){
            if(!in_array($k,$arr)) {
                $paramStr[] = $k . '=' . $v;
            }
        }
        $paramStrs = implode('&', $paramStr);
        $url_path = $url_path.'?'.$paramStrs;
    }
    return $url_path;
}

function page_param(){
    $param = request()->param();
    if (isset($param['_pjax'])){
        unset($param['_pjax']);
    }
    $res['query'] = $param;
    return $res;
}

//自定义生成文件方法名
//返回示例：49817531e904eca61b5917f5ebf12ffa.png
function getFileName(){
    $date = date("YmdHis");
    $time = microtime();
    $fileName = md5($date.'_'.current(explode(" ", $time)));
    return $fileName;
}

function authAction($rule, $cationType='create', $param=[]){
    if(!\app\admin\logic\User::checkHasPermission($rule,$cationType)){
        return '';
    }
    $cationTypes = [
        'create' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary xzrw'><i class='fa fa-save'></i> 新增</a>",
        'create1' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 查看事件分类</a>",
        'create2' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 查看事件</a>",
        'create19' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 查看员工</a>",
        'create3' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 查看配置</a>",
        'create4' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 查看固定积分</a>",
        'create5' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 我提交的奖扣</a>",
        'create6' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 我参与的奖扣</a>",
        'create7' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 我参与的任务</a>",
        'create8' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 我负责的任务</a>",
        'create9' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 任务审核</a>",
        'create10' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 公告审核</a>",
        'create11' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 任务发布</a>",
        'create12' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 公告发布</a>",
        'create13' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 任务大厅</a>",
        'create14' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 公告大厅</a>",
        'create15' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 审核</a>",
        'create16' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 请假申请</a>",
        'create17' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 新增(不包含专审事件)</a>",
        'create18' => "<a href='".url($rule, $param)."' class='btn btn-sm btn-primary'><i class='fa fa-save'></i> 新增(专审事件)</a>",
        'edit' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 编辑</a>",
        'edit1' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 配置本月人员</a>",
        'edit2' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 查看</a>",
        'edit3' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 编辑下个月配置</a>",
        'edit4' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 配置下个月人员</a>",
        'edit5' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 管理人员</a>",
        'edit6' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 移动</a>",
        'edit7' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 添加人员</a>",
        'edit8' => "<a class='btn btn-primary' href='".url($rule, $param)."'><i class='fa fa-edit'></i> 审核</a>",
        'show' => "<a class='btn btn-primary' href='".url($rule, $param)."'> 查看</a>",
        'show1' => "<a class='btn btn-primary' href='".url($rule, $param)."'> 事件分类</a>",
        //奖扣事件催审b  author:zj
        'audit_urge' => "<a class='btn btn-sm btn-success audit-urge' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-volume-up'></i> 催审</a>",
        'audit_sub' => "<a class='btn btn-sm btn-primary audit-sub' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-save'></i> 提交</a>",
        'audit_del' => "<a class='btn btn-sm btn-danger audit-del' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-trash'></i> 删除</a>",
        //奖扣事件催审e  author:zj

        'delete_one' => "<a class='btn btn-danger delete-one' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-trash'></i> 删除</a>",


        'create_arc' => "<a class=\"btn btn-primary\" href=\"".url($rule, $param)."\"><i class=\"fa fa-edit\"></i> 新增文章</a>",
        'delete_all' => "<a class=\"btn btn-sm btn-danger delete-all\" href=\"javascript:void(0);\" data-url=\"".url($rule)."\" ><i class=\"fa fa-trash\"></i> 删除</a>",
        'save' => "<button type=\"submit\" class=\"btn btn-info pull-right submits\" data-loading-text=\"&lt;i class='fa fa-spinner fa-spin '&gt;&lt;/i&gt; ".lang('submit')."\">".lang('submit')."</button>",
        'auth_user' => "<a class=\"btn btn-primary\" href=\"".url($rule, $param)."\"><i class=\"fa fa-edit\"></i> ".lang('auth_user')."</a>",
        'auth_group' => "<a class=\"btn btn-primary\" href=\"".url($rule, $param)."\"><i class=\"fa fa-edit\"></i> ".lang('auth_group')."</a>",
        'agree' => "<a class=\"btn btn-success\" onclick=\"return confirm('是否已确认给用户退完款？');\" href=\"".url($rule, $param)."\"><i class=\"fa fa-repeat\"></i> ".lang('agree')."</a>",
        'disagree' => "<a class=\"btn btn-danger\" href=\"".url($rule, $param)."\"><i class=\"fa fa-undo\"></i> ".lang('disagree')."</a>",
        'backup' => "<a class=\"btn btn-primary btn-sm delete-all\" href=\"javascript:void(0);\" data-url=\"".url($rule)."\" data-title=\"".lang('backup')."\"><i class=\"fa fa-save\"></i> ".lang('backup')."</a>",
//        'restore' => "<a class=\"btn btn-primary btn-xs delete-one\" href=\"javascript:void(0);\" data-url=\"".url($rule)."\" data-id=\"".$param."\" data-title=\"".lang('restore')."\"><i class=\"fa fa-rotate-left\"></i> ".lang('restore')."</a>",
        'dowonload' => "<a class=\"btn btn-warning btn-xs\" href=\"".url($rule, $param)."\"><i class=\"fa fa-download\"></i> ".lang('dowonload')."</a>",
//        'tokenapi' => "<a class=\"btn btn-danger btn-xs delete-one\" href=\"javascript:void(0);\" data-url=\"".url($rule)."\" data-id=\"".$param."\" data-title=\"".lang('tokenapi')."\"><i class=\"fa fa-lock\"></i> ".lang('tokenapi')."</a>",
//        'generate_document' => "<a class=\"btn btn-danger btn-xs delete-one\" href=\"javascript:void(0);\" data-url=\"".url($rule)."\" data-id=\"".$param."\" data-title=\"".lang('generate_document')."\"><i class=\"fa fa-book\"></i> ".lang('generate_document')."</a>",
        'view_document' => "<a class=\"btn btn-warning btn-xs\" href=\"".url($rule, $param)."\"><i class=\"fa fa-book\"></i> ".lang('view_document')."</a>",
        'activity_import'=>'<a href="javascript:;" class="btn btn-sm btn-primary" id="import_staff" >导入</a>',
        'review_import'=>'<a href="javascript:;" class="btn btn-sm btn-primary" id="import"><i class="fa fa-cloud-download"></i> 导入</a>',

        'audit_urge1' => "<a class='btn btn-sm btn-success audit-urge1' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-save'></i> 清除用户积分</a>",
        'ticket_change' => "<a class='btn btn-sm btn-success ticket_change' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-save'></i> 奖票转换</a>",
        'integral_cancellation' => "<a class='btn btn-sm btn-success integral_cancellation' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-save'></i> 作废</a>",
        'integral_recover' => "<a class='btn btn-sm btn-success integral_recover' href='javascript:void(0);' data-url='".url($rule)."' data-id='".(isset($param['id'])?$param['id']:0)."'><i class='fa fa-save'></i> 恢复</a>",
        'agree1' => "<a class=\"btn btn-success\" onclick=\"return confirm('确认清除用户积分？');\" href=\"".url($rule, $param)."\"><i class=\"fa fa-save\"></i> ".lang('清除用户积分')."</a>",
    ];
    $result = $cationTypes[$cationType];
    return $result;
}

//获取一条权限在权限列表生成一条tr的html
function getPermissionHtml($permission=[], $level=0, $params=[]){
    if(empty($permission) || !isset($permission["id"])){
        return '';
    }
    $prefixArr = [
        "",
        "&ensp;&ensp;&ensp;&ensp;|&ensp;",
        "&ensp;&ensp;&ensp;&ensp;|&ensp;&ensp;&ensp;&ensp;&ensp;├&ensp;"
    ];
    $type = isset($params["type"]) ? $params["type"] : 'tr';
    switch ($type){
        case 'option':
            $str =
                "<option "
                    ."value='".$permission['id']."' "
                    .($permission['status']?"":"disabled='disabled' ")
                    .($permission["id"]==$params["pid"]?"selected='selected' ":"")
                .">"
                    .$prefixArr[$level].$permission["title"]
                ."</option>";
            break;
        case 'tr':
        default:
            $str =
                "<tr>"
                    ."<td>".$prefixArr[$level].$permission["title"]."</td>"
                    ."<td>".$permission["url"]."</td>"
                    ."<td>".$permission["sort"]."</td>"
                    ."<td>".($permission["status"]?"是":"否")."</td>"
                    ."<td>"
                        .authAction("/".MODULE_NAME."/".CONTROLLER_NAME.'/edit','edit',["id"=>$permission["id"]])
                        .authAction("/".MODULE_NAME."/".CONTROLLER_NAME.'/delete','delete_one',["id"=>$permission["id"]])
                    ."</td>"
                ."</tr>";
            break;
    }
    return $str;
}

//组成无限级数的数据
function toTree($arr=[],$id=0, $children = 'list'){
    $list = [];
    foreach($arr as $k=>$v){
        if($v["pid"] == $id){
            unset($arr[$k]);
            $v["$children"] = toTree($arr, $v["id"], $children);
            $list[] = $v;
        }
    }
    return $list;
}

function table_sort($param){
    $url_path = '/'.request()->path();
    $faStr = 'fa-sort';
    $get = input('get.');
    if( isset($get['_pjax']) ){ unset($get['_pjax']); }

    if( isset($get['_sort']) ){   //判断是否存在排序字段
        $sortArr = explode(',', $get['_sort']);
        if ( $sortArr[0] == $param ){   //当前排序
            if ($sortArr[1] == 'asc'){
                $faStr = 'fa-sort-asc';
                $sort = 'desc';
            }elseif ($sortArr[1] == 'desc'){
                $faStr = 'fa-sort-desc';
                $sort = 'asc';
            }
            $get['_sort'] = $param.','.$sort;
        }else{   //非当前排序
            $get['_sort'] = $param.',asc';
        }
    }else{
        $get['_sort'] = $param.',asc';
    }
    $paramStr = [];
    foreach ($get as $k=>$v){
        $paramStr[] = $k.'='.$v;
    }
    $paramStrs = implode('&', $paramStr);
    $url_path = $url_path.'?'.$paramStrs;
    return "<a class=\"fa ".$faStr."\" href=\"".$url_path."\"></a>";
}

/**
 * 部门列表层级
 */
function organization_tree($tree_arr, $num=0){
    static $list = [];
    static $num = 0;
    foreach($tree_arr as $k=>$v){
        $list[] = ['id'=>$v['id'], 'name'=>$v['name'], 'num'=>$num];
        if($v['list']) {
            $num++;
            organization_tree($v['list'], $num);
        }
    }
    $num--;
    return $list;
}

/**
 * 事件类型列表层级
 */
function event_type_tree($tree_arr){
    static $list = [];
    foreach($tree_arr as $k=>$v){
        $list[] = ['id'=>$v['id'], 'name'=>$v['name']];
        if($v['list']) {
            event_type_tree($v['list']);
        }
    }
    return $list;
}

/**
 * 获取部门下的子部门id
 * @param $id 要查询下级的部门id数组
 * @param $com_id 企业id
 */
function department_child($id, $com_id){
    static $ids = [];
    $ids = array_merge($ids, $id);
    $child_ids = db('com_department')->where(['pid'=>['in', $id], 'com_id'=>$com_id])->column('id');
    if($child_ids){
        department_child($child_ids, $com_id);
    }
    return array_unique($ids);
}
//返回json数据
function return_json($code,$msg='成功',$data=[]){
    $return_data = array(
        'code'=>$code,
        'msg'=>$msg,
        'data'=>$data
    );
    return json($return_data);
}